package biz.datalk.industrialland.common.util;

import biz.datalk.commons.utils.BasicParserUtils;
import biz.datalk.industrialland.common.def.CommonConstant;
import biz.datalk.industrialland.common.exception.ValidateException;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tarofang@163.com
 * @date 2024-02-20
 */
@Slf4j
public class EntityIdUtils {

    public static List<Long> parseIds(String idStr) {
        return parseIds(idStr, CommonConstant.SYSIDS_SEPARATOR, Boolean.TRUE);
    }

    public static List<Long> parseIds(String idStr, String delim, boolean throwException) {
        String[] strings = StringUtils.splitByWholeSeparatorPreserveAllTokens(idStr, delim);
        if (ArrayUtils.isEmpty(strings)) {
            return Lists.newArrayList();
        }
        ArrayList<Long> list = Lists.newArrayList();
        for (String string : strings) {
            Long e = BasicParserUtils.parseLong(string, 0L);
            if (throwException && e <= 0L) {
                log.warn("Invalid idStr. [item={}, idStr={}]", string, idStr);
                throw new ValidateException("非法的ID值");
            }
            list.add(e);
        }
        return list;
    }

}
